﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.HandleDictionary`1
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;

namespace Microsoft.InfoCards
{
  internal class HandleDictionary<TVal>
  {
    private const int m_MaxSessionCount = 1073741823;
    private Dictionary<int, TVal> m_internalDictionary;
    private Random m_random;

    public int MaxSize
    {
      get
      {
        return 1073741823;
      }
    }

    public TVal this[int Key]
    {
      get
      {
        return this.m_internalDictionary[Key];
      }
      set
      {
        if (!this.ContainsHandle(Key))
          throw InfoCardTrace.ThrowHelperError((Exception) new KeyNotFoundException());
        this.m_internalDictionary[Key] = value;
      }
    }

    public HandleDictionary()
    {
      this.m_internalDictionary = new Dictionary<int, TVal>();
      this.m_random = new Random();
    }

    public int GetNewHandle()
    {
      if (this.m_internalDictionary.Count >= 1073741823)
        throw InfoCardTrace.ThrowHelperError((Exception) new MaxSessionCountExceededException());
      int key = this.m_random.Next();
      while (this.m_internalDictionary.ContainsKey(key) || key == 0)
        key = this.m_random.Next();
      this.m_internalDictionary[key] = default (TVal);
      return key;
    }

    public bool Remove(int key)
    {
      return this.m_internalDictionary.Remove(key);
    }

    public bool ContainsHandle(int key)
    {
      return this.m_internalDictionary.ContainsKey(key);
    }
  }
}
